﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LibFriesland_Campina_CRM;
using System.Data;
using CrystalDecisions.Shared;

public partial class ViewReport : System.Web.UI.Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
        try
        {
            //lay ten report can xem
            string report = Request.QueryString["report"].ToString();
            switch (report)
            {
                case "viewNaSale": displayNaSaleReport();
                    break;
                case "viewSupSale": displaySupSaleReport();
                    break;
                case "viewShopper": displayShopperReport();
                    break;
                case "viewShopSale": displayShopSaleReport();
                    break;
                case "viewWareHousing": displayWareHousingbyShopReport();
                    break;
                case "viewProductInShop": displayProductInShopReport();
                    break;
                case "viewPriceAudit": displayPriceAuditReport();
                    break;
            }
        }
        catch (Exception ex)
        {
            Response.Redirect("login.aspx");
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["account"] == null)
            Response.Redirect("login.aspx");
    }

    //Hien thi bao cao thong tin khach hang
    private void displayShopperReport()
    {
        DateTime fromDate = Convert.ToDateTime(Thuvien.myConvertStringToShortDate(Request.QueryString["fromDate"]));
        DateTime toDate = Convert.ToDateTime(Thuvien.myConvertStringToShortDate(Request.QueryString["toDate"]));
        LibFriesland_Campina_CRM.Dataset.DatasetShopperStatistics ds = BizShopper.getShopperStatistics(fromDate, toDate);
        LibFriesland_Campina_CRM.Report.ReportShopper report = new LibFriesland_Campina_CRM.Report.ReportShopper();

        //Truyen tham so cho Report
        ParameterFields paramFieldArray = new ParameterFields();
        ParameterField paramFieldFromDate = new ParameterField();
        ParameterField paramFieldToDate = new ParameterField();

        paramFieldFromDate.Name = "pFromDate";
        paramFieldToDate.Name = "pToDate";

        paramFieldFromDate.CurrentValues.AddValue(fromDate.ToString("dd/MM/yyyy"));
        paramFieldToDate.CurrentValues.AddValue(toDate.ToString("dd/MM/yyyy"));

        paramFieldArray.Add(paramFieldFromDate);
        paramFieldArray.Add(paramFieldToDate);

        myCrv.ParameterFieldInfo = paramFieldArray;

        report.SetDataSource(ds); //Gan nguon cho bao cao
        myCrv.HasToggleParameterPanelButton = false;
        myCrv.ReportSource = report;
        myCrv.DataBind();
    }
    
    //Hien thi bao cao doanh so ban hang cua NA
    private void displayNaSaleReport()
    {
        string by_na = Request.QueryString["na"].ToString();
        DateTime fromDate = Convert.ToDateTime(Thuvien.myConvertStringToShortDate(Request.QueryString["fromDate"]));
        DateTime toDate = Convert.ToDateTime(Thuvien.myConvertStringToShortDate(Request.QueryString["toDate"]));

        LibFriesland_Campina_CRM.Dataset.DatasetNaSale ds = BizInvoiceDetail.getSalebyDateNa(fromDate, toDate, by_na);
        LibFriesland_Campina_CRM.Report.ReportNASale report = new LibFriesland_Campina_CRM.Report.ReportNASale();

        //Truyen tham so cho Report
        ParameterFields paramFieldArray = new ParameterFields();
        ParameterField paramFieldNa = new ParameterField();
        ParameterField paramFieldFromDate = new ParameterField();
        ParameterField paramFieldToDate = new ParameterField();

        paramFieldNa.Name = "pNa";
        paramFieldFromDate.Name = "pFromDate";
        paramFieldToDate.Name = "pToDate";

        BizNA na = BizNA.getNabyID(by_na);
        paramFieldNa.CurrentValues.AddValue(na.name + " - " + by_na);
        paramFieldFromDate.CurrentValues.AddValue(fromDate.ToString("dd/MM/yyyy"));
        paramFieldToDate.CurrentValues.AddValue(toDate.ToString("dd/MM/yyyy"));

        paramFieldArray.Add(paramFieldNa);
        paramFieldArray.Add(paramFieldFromDate);
        paramFieldArray.Add(paramFieldToDate);

        myCrv.ParameterFieldInfo = paramFieldArray;
        
        report.SetDataSource(ds);
        myCrv.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None;
        myCrv.HasToggleParameterPanelButton = false;
        myCrv.ReportSource = report;
        myCrv.DataBind();
    }

    //Hien thi bao cao doanh so ban hang cua Supervisor
    private void displaySupSaleReport()
    {
        string by_sup = Request.QueryString["sup"].ToString();
        DateTime fromDate = Convert.ToDateTime(Thuvien.myConvertStringToShortDate(Request.QueryString["fromDate"]));
        DateTime toDate = Convert.ToDateTime(Thuvien.myConvertStringToShortDate(Request.QueryString["toDate"]));

        LibFriesland_Campina_CRM.Dataset.DatasetSupSale ds = BizInvoiceDetail.getSalebyDateSup(fromDate, toDate, by_sup);
        LibFriesland_Campina_CRM.Report.ReportSupSale report = new LibFriesland_Campina_CRM.Report.ReportSupSale();

        //Truyen tham so cho Report
        ParameterFields paramFieldArray = new ParameterFields();
        ParameterField paramFieldNa = new ParameterField();
        ParameterField paramFieldFromDate = new ParameterField();
        ParameterField paramFieldToDate = new ParameterField();

        paramFieldNa.Name = "pSup";
        paramFieldFromDate.Name = "pFromDate";
        paramFieldToDate.Name = "pToDate";

        BizSupervisor sup = BizSupervisor.getSupbyID(by_sup);
        paramFieldNa.CurrentValues.AddValue(sup.name + " - " + sup.sup_charid);
        paramFieldFromDate.CurrentValues.AddValue(fromDate.ToString("dd/MM/yyyy"));
        paramFieldToDate.CurrentValues.AddValue(toDate.ToString("dd/MM/yyyy"));

        paramFieldArray.Add(paramFieldNa);
        paramFieldArray.Add(paramFieldFromDate);
        paramFieldArray.Add(paramFieldToDate);

        myCrv.ParameterFieldInfo = paramFieldArray;

        report.SetDataSource(ds);
        myCrv.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None;
        myCrv.HasToggleParameterPanelButton = false;
        myCrv.ReportSource = report;
        myCrv.DataBind();
    }

    //Hien thi bao cao doanh so ban hang cua Shop
    private void displayShopSaleReport()
    {
        DateTime fromDate = Convert.ToDateTime(Thuvien.myConvertStringToShortDate(Request.QueryString["fromDate"]));
        DateTime toDate = Convert.ToDateTime(Thuvien.myConvertStringToShortDate(Request.QueryString["toDate"]));
        LibFriesland_Campina_CRM.Dataset.DatasetShopSale ds = BizInvoiceDetail.getSalebyDateShop(fromDate, toDate);
        LibFriesland_Campina_CRM.Report.ReportShopSale report = new LibFriesland_Campina_CRM.Report.ReportShopSale();

        //Truyen tham so cho Report
        ParameterFields paramFieldArray = new ParameterFields();
        ParameterField paramFieldFromDate = new ParameterField();
        ParameterField paramFieldToDate = new ParameterField();

        paramFieldFromDate.Name = "pFromDate";
        paramFieldToDate.Name = "pToDate";

        paramFieldFromDate.CurrentValues.AddValue(fromDate.ToString("dd/MM/yyyy"));
        paramFieldToDate.CurrentValues.AddValue(toDate.ToString("dd/MM/yyyy"));

        paramFieldArray.Add(paramFieldFromDate);
        paramFieldArray.Add(paramFieldToDate);

        myCrv.ParameterFieldInfo = paramFieldArray;

        report.SetDataSource(ds); //Gan nguon cho bao cao
        myCrv.HasToggleParameterPanelButton = false;
        myCrv.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None;
        myCrv.ReportSource = report;
        myCrv.DataBind();
    }

    //Hien thi bao cao nhap kho cua cua hang
    private void displayWareHousingbyShopReport()
    {
        DateTime fromDate = Convert.ToDateTime(Thuvien.myConvertStringToShortDate(Request.QueryString["fromDate"]));
        DateTime toDate = Convert.ToDateTime(Thuvien.myConvertStringToShortDate(Request.QueryString["toDate"]));
        LibFriesland_Campina_CRM.Dataset.DatasetWareHousing ds = BizWareHousingDetail.getWareHousingbyDateShop(fromDate, toDate);
        LibFriesland_Campina_CRM.Report.ReportWareHousingbyShop report = new LibFriesland_Campina_CRM.Report.ReportWareHousingbyShop();

        //Truyen tham so cho Report
        ParameterFields paramFieldArray = new ParameterFields();
        ParameterField paramFieldFromDate = new ParameterField();
        ParameterField paramFieldToDate = new ParameterField();

        paramFieldFromDate.Name = "pFromDate";
        paramFieldToDate.Name = "pToDate";

        paramFieldFromDate.CurrentValues.AddValue(fromDate.ToString("dd/MM/yyyy"));
        paramFieldToDate.CurrentValues.AddValue(toDate.ToString("dd/MM/yyyy"));

        paramFieldArray.Add(paramFieldFromDate);
        paramFieldArray.Add(paramFieldToDate);

        myCrv.ParameterFieldInfo = paramFieldArray;

        report.SetDataSource(ds); //Gan nguon cho bao cao
        myCrv.HasToggleParameterPanelButton = false;
        myCrv.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None;
        myCrv.ReportSource = report;
        myCrv.DataBind();
    }

    //Hien thi bao cao san pham ton kho cua cua hang
    private void displayProductInShopReport()
    {
        DateTime date = Convert.ToDateTime(Thuvien.myConvertStringToShortDate(Request.QueryString["date"]));
        LibFriesland_Campina_CRM.Dataset.DatasetProductInShop ds = BizProductInShopDiary.getProductQuantityInShopbyDate(date);

        if (ds.Tables["PRODUCT_IN_SHOP"].Rows.Count > 0)
        {
            LibFriesland_Campina_CRM.Report.ReportProducInShop report = new LibFriesland_Campina_CRM.Report.ReportProducInShop();

            //Truyen tham so cho Report
            ParameterFields paramFieldArray = new ParameterFields();
            ParameterField paramFieldDate = new ParameterField();

            paramFieldDate.Name = "pDate";

            paramFieldDate.CurrentValues.AddValue(date.ToString("dd/MM/yyyy"));
            paramFieldArray.Add(paramFieldDate);

            myCrv.ParameterFieldInfo = paramFieldArray;

            report.SetDataSource(ds); //Gan nguon cho bao cao
            myCrv.HasToggleParameterPanelButton = false;
            myCrv.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None;
            myCrv.ReportSource = report;
            myCrv.DataBind();
        }
        else
            Response.Write("<script>alert('Không có dữ liệu')</script>"); 
    }

    //Hien thi bao cao gia ban le cua cua hang Price Audit
    private void displayPriceAuditReport()
    {
        DateTime date = Convert.ToDateTime(Thuvien.myConvertStringToShortDate(Request.QueryString["date"]));
        LibFriesland_Campina_CRM.Dataset.DatasetPriceAudit ds = BizProductInShopDiary.getPriceAudit(date);

        if (ds.Tables["PRODUCT_IN_SHOP"].Rows.Count > 0)
        {
            LibFriesland_Campina_CRM.Report.ReportPriceAudit report = new LibFriesland_Campina_CRM.Report.ReportPriceAudit();

            //Truyen tham so cho Report
            ParameterFields paramFieldArray = new ParameterFields();
            ParameterField paramFieldDate = new ParameterField();

            paramFieldDate.Name = "pDate";

            paramFieldDate.CurrentValues.AddValue(date.ToString("dd/MM/yyyy"));
            paramFieldArray.Add(paramFieldDate);

            myCrv.ParameterFieldInfo = paramFieldArray;

            report.SetDataSource(ds); //Gan nguon cho bao cao
            myCrv.HasToggleParameterPanelButton = false;
            myCrv.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None;
            myCrv.ReportSource = report;
            myCrv.DataBind();
        }
        else
            Response.Write("<script>alert('Không có dữ liệu')</script>");
    }
}